package com.example.multitenant.multitenant.config

import org.springframework.context.annotation.Configuration
import org.springframework.core.task.TaskDecorator
import org.springframework.scheduling.annotation.AsyncConfigurerSupport
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor
import java.util.concurrent.Executor


/**
 * @author xiang
 * @Date 2022/9/15 19:23
 **/
//@Configuration
class AsyncConfig : AsyncConfigurerSupport() {
    override fun getAsyncExecutor(): Executor {
        val executor = ThreadPoolTaskExecutor()
        executor.corePoolSize = 7
        executor.maxPoolSize = 42
        executor.queueCapacity = 11
        executor.setThreadNamePrefix("TenantTaskExecutor-")
        executor.setTaskDecorator { runnable ->
            val tenant = TenantContext.getTenantId()
            Runnable {
                TenantContext.setTenantId(tenant)
                runnable.run()
            }
        }
        executor.initialize()
        return executor
    }
}